package com.example.handdet;

import android.graphics.Bitmap;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {
    private final static String TAG = "Utils";
    /**
     * 图片目录
     */
    public static final String SDCARD_PATH = "/storage/emulated/0/GestureImg/";


    public static String getDownloadPath(String nextPath) {
        String localFilePath = SDCARD_PATH + nextPath;
        File hiddenFolder = new File(localFilePath);
        if (!hiddenFolder.exists()) {
            hiddenFolder.mkdir();
        }
        return localFilePath;
    }

    /**
     * 保存bitmap到SD卡
     *
     * @param bitmap
     */
    public static void saveBitmapToSDCard(Bitmap bitmap, String result) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
        Date date = new Date(System.currentTimeMillis());
        String time = simpleDateFormat.format(date);
        String fileName = result + "_" + time + "_" + ".jpg";
        File jia = new File(getDownloadPath(result));
        if (!jia.exists()) {
            jia.mkdirs();
        }
        File myCaptureFile = new File(jia + "/" + fileName);
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        try {
            bos.flush();
            bos.close();
            Log.i(TAG, "Save image to SDCard, "+ myCaptureFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
